package com.yuke.cloud.service.cmc.controller.frontend;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.cmc.dto.CmsBoundDto;
import com.yuke.cloud.service.cmc.dto.CmsCouponSpecDto;
import com.yuke.cloud.service.cmc.entity.CmsCouponSpec;
import com.yuke.cloud.service.cmc.service.CmsCouponSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by wg on 2019/09/25.
 */
@Api(value = "WEB - CmsCouponSpecController", tags = "处理CmsCouponSpec相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/coupon_spec")
public class CmsCouponSpecController extends BaseController {
    @Resource
    private CmsCouponSpecService cmsCouponSpecService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsCouponSpec", value = "该数据记录的POJO") @RequestBody CmsCouponSpec cmsCouponSpec) {
        cmsCouponSpecService.save(cmsCouponSpec);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsCouponSpecService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsCouponSpec", value = "需要修改的内容") @RequestBody CmsCouponSpec cmsCouponSpec) {
        cmsCouponSpecService.update(cmsCouponSpec);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsCouponSpec> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsCouponSpec cmsCouponSpec = cmsCouponSpecService.findById(id);
        return WrapMapper.ok(cmsCouponSpec);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsCouponSpec>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                 @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<CmsCouponSpec> list = cmsCouponSpecService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }


    @PostMapping("/binding")
    @ApiOperation(httpMethod = "POST", value = "获取商品已绑定记录")
    public Wrapper<PageInfo<CmsCouponSpecDto>> getBoundGoods(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                             @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                             @RequestBody CmsCouponSpec cmsCouponSpec) {
        PageHelper.startPage(page, size);
        List<CmsCouponSpecDto> list = cmsCouponSpecService.getBindingGoods(cmsCouponSpec);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    public List<CmsCouponSpecDto> getCouponSpecListByCouponId(@RequestParam(value = "CouponId") Long couponId) {
        return cmsCouponSpecService.getCouponSpecListByCouponId(couponId);
    }

    @PostMapping("/getGoods")
    @ApiOperation(httpMethod = "GET", value = "获取商品未绑定选择列表")
    public Wrapper<PageInfo<CmsCouponSpecDto>> getGoodsList(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                            @RequestBody CmsCouponSpecDto cmsCouponSpecDto) {
        PageHelper.startPage(page, size);
        List<CmsCouponSpecDto> goodsList = cmsCouponSpecService.getGoodsList(cmsCouponSpecDto);
        PageInfo pageInfo = new PageInfo(goodsList);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/addGoods")
    @ApiOperation(httpMethod = "POST", value = "批量添加商deleteBoundGoods品绑定")
    public Wrapper addBoundGoods(@ApiParam(name = "cmsCouponSpec", value = "该数据记录的POJO") @RequestBody List<CmsCouponSpec> cmsCouponSpec) {
        for (CmsCouponSpec list:cmsCouponSpec) {
            list.setUpdateUser(getLoginAuthDto().getUserId());
            cmsCouponSpecService.save(list);
        }
        return WrapMapper.ok();
    }

    @DeleteMapping("/deleteBoundGoods/{specId}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id解除优惠券绑定)")
    public Wrapper deleteBoundGoods(@ApiParam(name = "id", value = "记录id")@PathVariable Long specId ){
        cmsCouponSpecService.deleteBoundGoods(specId);
        return WrapMapper.ok();
    }

    @DeleteMapping("/deleteBoundGoodsList")
    @ApiOperation(httpMethod = "DELETE", value = "根据id批量解除优惠券绑定)")
    public Wrapper deleteBoundGoodsList(@ApiParam(name = "id", value = "记录id") @RequestBody List<CmsBoundDto> cmsBoundDto){
        List<Long> specIds = cmsBoundDto.stream().map(CmsBoundDto::getSpecId).map(specId -> Long.valueOf(specId)).collect(Collectors.toList());
        cmsCouponSpecService.deleteBoundGoodsList(specIds);
        return WrapMapper.ok();
    }





}
